$(function(){
    getUserInfo()
    /* 需求二:点击退出 */
    $('#logout').on('click',function(){
        layer.confirm('确定退出咩', {icon: 3, title:'提示'}, function(index){
            //do something
            localStorage.removeItem('token')
            location.href='/login.html'
            layer.close(index);
          });
    })
})
/* 获取用户信息渲染头像封装函数 */
/* 必须是全局函数,不是局部函数 */
function getUserInfo(){
    axios({
        method:'GET',
        url:'/my/userinfo'
    }).then(({data:res})=>{
        if(res.status!==0){
            return layui.layer.msg(res.message)
        }
        /* 渲染 */
        renderAvatar(res.data)
    })
}
function renderAvatar(user){
    let name=user.nickname || user.username
    $('.welcome').html('欢迎&nbsp;'+name)
    if(user.user_pic!==null){
        $('.avatar-text').hide()
        $('.layui-nav-img').show().attr('src',user.user_pic)
    }else{
        let first=name[0].toUpperCase()
        $('.avatar-text').show().html(first)
        $('.layui-nav-img').hide()
    }
}